import pandas as pd
import matplotlib.pyplot as plt
import pyecharts.options as opts
from pyecharts.charts import Pie
class Salary:
    def __init__(self):
        # 1：数据采集
        filepath = 'data/银行用户信息.txt'
        user_infos = pd.read_table(filepath, sep=',', header=None, encoding='utf-8')
        print(user_infos.loc[0:3, 0:5])  # 冒号之前横切冒号之后竖切，：连续切[,]
        namelist = ['编号', '姓名', '性别', '身高', '生日', '籍贯', '身高', '开卡时间', '余额']
        nolist = list(range(9))
        headDist = dict(zip(nolist, namelist))
        user_infos.reset_index()
        user_infos.rename(columns=headDist, inplace=True)
        print(user_infos.loc[0:4, ['姓名', '性别', '余额']])
        # 2：数据预处理
        groupByseries = user_infos['余额'].groupby(user_infos['籍贯']).mean()
        # 3:数据可视化
        """
        Gallery 使用 pyecharts 1.1.0
        参考地址: https://echarts.apache.org/examples/editor.html?c=pie-doughnut

        目前无法实现的功能:

        1、迷之颜色映射的问题
        """
        x_data = list(groupByseries.keys())
        y_data = list(groupByseries)
        data_pair = [list(z) for z in zip(x_data, y_data)]
        data_pair.sort(key=lambda x: x[1])
        (
            Pie(init_opts=opts.InitOpts(width="1600px", height="800px", bg_color="#2c343c"))
                .add(
                series_name="访问来源",
                data_pair=data_pair,
                rosetype="radius",
                radius="55%",
                center=["50%", "50%"],
                label_opts=opts.LabelOpts(is_show=False, position="center"),
            )
                .set_global_opts(
                title_opts=opts.TitleOpts(
                    title="Customized Pie",
                    pos_left="center",
                    pos_top="20",
                    title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
                ),
                legend_opts=opts.LegendOpts(is_show=False),
            )
                .set_series_opts(
                tooltip_opts=opts.TooltipOpts(
                    trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
                ),
                label_opts=opts.LabelOpts(color="rgba(255, 255, 255, 0.3)"),
            )
                .render("customized_pie.html")
        )
Salary()
